/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.procesos;

import pe.com.unidades.Placa;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;

/**
 *
 * @author romulogalindo
 */
public class ProcesadorSat extends Proceso implements Runnable {

    private Placa pla;

    public ProcesadorSat(Placa pla) {
        this.pla = pla;
    }

    public Placa getPla() {
        return pla;
    }

    public void setPla(Placa pla) {
        this.pla = pla;
    }

    @Override
    public void run() {
        String res;
        org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
        try {
            HttpGet http = new HttpGet("http://sat.gob.pe");
            HttpResponse resp = client.execute(http);
            String rpta1 = IOUtils.toString(resp.getEntity().getContent(), "UTF-8");

            HttpPost http2 = new HttpPost("http://sat.gob.pe/Websitev8/InicioOV.aspx?t=2&v=D1S615");
            http2.getParams().setParameter("http.protocol.handle-redirects", true);
            client.setRedirectStrategy(new DefaultRedirectStrategy() {
                public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
                    boolean isRedirect = false;
                    try {
                        System.out.println("request:" + request.getRequestLine());
                        isRedirect = super.isRedirected(request, response, context);
                    } catch (ProtocolException e) {
                        e.printStackTrace();
                    }
                    if (!isRedirect) {
                        int responseCode = response.getStatusLine().getStatusCode();
                        if (responseCode == 301 || responseCode == 302) {
                            return true;
                        }
                    }
                    return isRedirect;
                }
            });
            org.apache.http.HttpResponse resp2 = client.execute(http2);

            InputStream instream2 = resp2.getEntity().getContent();
            String rpta2 = IOUtils.toString(instream2, "UTF-8");
            rpta2 = rpta2.substring(rpta2.indexOf("bienvenida.aspx?"), rpta2.length());
            rpta2 = rpta2.substring(0, rpta2.indexOf("FRAMEBORDER") - 2);

            String rk = "https://www.sat.gob.pe/homesite2007/modulos/" + rpta2.replace("bienvenida", "papeletas");
            HttpGet http3 = new HttpGet(rk);
            org.apache.http.HttpResponse resp3 = client.execute(http3);
            InputStream instream3 = resp3.getEntity().getContent();
            String rpta3 = IOUtils.toString(instream3, "UTF-8");

            rpta3 = rpta3.substring(rpta3.indexOf("__VIEWSTATE") + 12, rpta3.length());
            rpta3 = rpta3.substring(rpta3.indexOf("value") + 7, rpta3.length());
            String __VIEWSTATE = rpta3.substring(0, rpta3.indexOf("\""));

            rpta3 = rpta3.substring(rpta3.indexOf("__EVENTVALIDATION") + 18, rpta3.length());
            rpta3 = rpta3.substring(rpta3.indexOf("value") + 7, rpta3.length());
            String __EVENTVALIDATION = rpta3.substring(0, rpta3.indexOf("\""));
            /**
             *
             */
            NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "");
            NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", "");
            NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE);
            NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
            NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", __EVENTVALIDATION);
            NameValuePair p_6 = new BasicNameValuePair("ctl00$cplPrincipal$txtPlaca", "D1S615");
            NameValuePair p_6_2 = new BasicNameValuePair("ctl00$cplPrincipal$txtPlaca", "D7C615");
            NameValuePair p_7 = new BasicNameValuePair("ctl00$cplPrincipal$btnBuscar", "Buscar");
            NameValuePair p_8 = new BasicNameValuePair("ctl00$cplPrincipal$txtPapeleta", "");

            List loginNvps = new ArrayList();
            loginNvps.add(p_1);
            loginNvps.add(p_2);
            loginNvps.add(p_3);
            loginNvps.add(p_4);
            loginNvps.add(p_5);
            loginNvps.add(p_6);
            loginNvps.add(p_7);
            loginNvps.add(p_8);

//            /**
//             *
//             */
            HttpPost http4 = new HttpPost(rk);
            http4.setEntity(new UrlEncodedFormEntity(loginNvps, "UTF-8"));
            org.apache.http.HttpResponse resp4 = client.execute(http4);
            String rpta4 = IOUtils.toString(resp4.getEntity().getContent(), "UTF-8");
            rpta4 = rpta4.substring(rpta4.indexOf("Monto total a pagar"), rpta4.length());
            rpta4 = rpta4.substring(rpta4.indexOf("<b>") + 3, rpta4.length());
            rpta4 = rpta4.substring(0, rpta4.indexOf("</b>"));

            System.out.println("rpta4@@@@:" + rpta4);

            loginNvps = new ArrayList();
            loginNvps.add(p_1);
            loginNvps.add(p_2);
            loginNvps.add(p_3);
            loginNvps.add(p_4);
            loginNvps.add(p_5);
            loginNvps.add(p_6_2);
            loginNvps.add(p_7);
            loginNvps.add(p_8);

            http4 = new HttpPost(rk);
            http4.setEntity(new UrlEncodedFormEntity(loginNvps, "UTF-8"));
            rpta4 = IOUtils.toString(client.execute(http4).getEntity().getContent(), "UTF-8");
//            System.out.println("rpta4:"+rpta4);
            rpta4 = rpta4.substring(rpta4.indexOf("Monto total a pagar"), rpta4.length());
            rpta4 = rpta4.substring(rpta4.indexOf("<b>") + 3, rpta4.length());
            rpta4 = rpta4.substring(0, rpta4.indexOf("</b>"));

            System.out.println("rpta4@@@@:" + rpta4);

            //cel.setNumeroRPM(rpta3.trim());
        } catch (Exception ep) {
            System.out.println(">" + ep.getMessage());
//            return "";
        }

    }

}
